//
//  main.m
//  objc-demo
//
//  Created by i on 2021/12/12.
//

#import <Foundation/Foundation.h>

@protocol LifeProtocol <NSObject>
@required
- (void)born;
@end

@interface Animal : NSObject<LifeProtocol> {
int _age1;
//@protected,@private
@public int _age2;
}
@property (assign,nonatomic,getter=getName,readwrite) NSString *name;
@property (assign,getter=getAge) NSInteger age;
- (void) yellWithWord:(NSString *)word;
- (id)alloc;
- (void)dealloc;
+ (void)die;
- (id)init;
@end

@implementation Animal
#pragma mark - life protocol
- (void)born {
    NSLog(@"born %@",[self getName]);
}
- (void) yellWithWord:(NSString *)word {
    NSLog(@"yell %@ %@",word,[self getName]);
}
- (void)dealloc {
    NSLog(@"delloc delloc");
}
+ (void)die {
    NSLog(@"die");
}
@end

@interface Animal (JumpCat)
- (void)jumpWithMeter:(NSNumber *)meter;
@end
@implementation Animal (JumpCat)
- (void)jumpWithMeter:(NSNumber *)meter {
    NSLog(@"%@ jumpWithMeter %@ meter with age %i",[self getName],meter,[self getAge]);
}
@end
@interface Dog : Animal

@end
@implementation Dog:Animal

- (id)init {
    if(self=[super init]){
        NSLog(@"parent %@ self%@",NSStringFromClass([super class]),NSStringFromClass([self class]));
        
    }
    return self;
}

@end



int main(int argc, const char * argv[]) {
    @autoreleasepool {
        NSLog(@"sandbox dir %@",NSHomeDirectory());
        
        NSString *documentsPathW =NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES)[0];
        //写入文件
        if (!documentsPathW) {
            NSLog(@"目录未找到");
            }else {
            NSString *filePath = [documentsPathW stringByAppendingPathComponent:@"test.txt"];
            NSArray *array = [NSArray arrayWithObjects:@"code",@"change", @"world", @"OK", @"", @"是的", nil];
                
            [array writeToFile:filePath atomically:YES];
        }


        
        NSString *documentsPathR = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES)[0];
        NSString *readPath = [documentsPathR stringByAppendingPathComponent:@"test.txt"];
        NSArray *fileContent = [[NSArray alloc] initWithContentsOfFile:readPath];
        NSLog(@"文件内容：%@",fileContent);

      
        
        
        Animal *an=[[Animal alloc] init];
        an.age=10;
        an.name=@"horse";
        
        NSMutableArray *narr1=[[NSMutableArray alloc] init];
        [narr1 addObject:@"pen"];
        
        NSMutableArray *narr2=[[NSMutableArray alloc] initWithObjects:@"car",@"tree", nil];
        [an born];
        Dog *dog=[Dog new];
        [dog yellWithWord:@"wang"];
        Animal *an2=[[Animal alloc] init];
        an2.name=@"duck";
        an2.age=5;
        [Animal die];
        
        [an jumpWithMeter:[[NSNumber alloc] initWithInt:5]];
        [an yellWithWord:@"wuwu"];
        
        [an2 yellWithWord:@"gaga"];
        // insert code here...
        NSLog(@"Hello, World! %@",narr1);
        
        //nsobject runtime
        [an isKindOfClass:[Animal class]];
        
        
    }
    
    return 0;
}
